﻿using System;
using System.Web;
using IterationLife.Datas;
using IterationLife.PlatformBackEnd.AppServices.Contracts.Systems;
using IterationLife.PlatformBackEnd.AppServices.Dtos.Systems;
using IterationLife.System.Models;
using IterationLife.System.Queries;
using IterationLife.System.Repositories;
using IterationLife.System.Services;
using Util.Applications;
using Util.Datas.Queries;

namespace IterationLife.PlatformBackEnd.AppServices.Impl.Systems
{
    /// <summary>
    /// 附件服务
    /// </summary>
    public class AttachmentService : ApplicationServiceBase<Attachment, AttachmentDto, AttachmentQuery>, IAttachmentService
    {
        /// <summary>
        /// 附件管理
        /// </summary>
        protected IAttachmentManager AttachmentManager { get; set; }

        /// <summary>
        /// 附件仓储
        /// </summary>
        protected IAttachmentRepository AttachmentRepository { get; set; }

        /// <summary>
        /// 初始化附件服务
        /// </summary>
        /// <param name="unitOfWork">工作单元</param>
        /// <param name="attachmentManager">附件管理</param>
        /// <param name="attachmentRepository">附件仓储</param>
        public AttachmentService(IApplicationUnitOfWork unitOfWork, IAttachmentRepository attachmentRepository, IAttachmentManager attachmentManager)
            : base(unitOfWork, attachmentRepository)
        {
            AttachmentManager = attachmentManager;
            AttachmentRepository = attachmentRepository;
        }


        /// <summary>
        /// 转换为数据传输对象
        /// </summary>
        /// <param name="entity">实体</param>
        protected override AttachmentDto ToDto(Attachment entity)
        {
            return entity.ToDto();
        }

        /// <summary>
        /// 转换为实体
        /// </summary>
        /// <param name="dto">数据传输对象</param>
        protected override Attachment ToEntity(AttachmentDto dto)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 创建查询对象
        /// </summary>
        /// <param name="param">附件查询实体</param>
        protected override IQueryBase<Attachment> CreateQuery(AttachmentQuery param)
        {
            return new Query<Attachment>(param);
        }

        /// <summary>
        /// 上传
        /// </summary>
        public AttachmentDto Upload(string id)
        {
            var entity = AttachmentManager.Upload(id ?? "file");
            UnitOfWork.Commit();
            return entity.ToDto();
        }

        /// <summary>
        /// 上传
        /// </summary>
        public AttachmentDto Upload(HttpPostedFile httpPostedFile)
        {
            var entity = AttachmentManager.Upload(httpPostedFile.InputStream, httpPostedFile.ContentLength, httpPostedFile.FileName);
            UnitOfWork.Commit();
            return entity.ToDto();
        }

        /// <summary>
        /// 上传
        /// </summary>
        public AttachmentDto Upload(byte[] fileBytes, int size, string fileName)
        {
            var entity = AttachmentManager.Upload(fileBytes, size, fileName);
            UnitOfWork.Commit();
            return entity.ToDto();
        }

    }
}